package ch.trainingtest.dao;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.Query;

import ch.trainingtest.base.dao.BaseDao;
import ch.trainingtest.common.NamedQueryConstants;
import ch.trainingtest.entity.User;

@Stateless
public class UserDao extends BaseDao<User> {
	public User findUserByUsernameAndPassword(String username, String password) {
		Query query = getEm().createNamedQuery(NamedQueryConstants.USER_FIND_USER_BY_USERNAME_PASSWORD);
		query.setParameter("pUsername", username);
		query.setParameter("pPassword", password);
		User result = (User) query.getSingleResult();
		return result;
	}
	
	public void addUser(String username, String password) {
		User user = new User(username, password);
		persist(user);
	}
	
	public List<User> getAllUsers() {
		Query query = getEm().createNamedQuery(NamedQueryConstants.USER_SELECT_ALL_USER);
		List<User> result = query.getResultList();
		return result;
		
	}
	
	public User findUserByID(int id) {
		return find(id);
	}
}
